//
//  TabViewBrowser.m
//  etionUI
//
//  Created by wangjian on 8/19/13.
//
//

#import "TabViewBrowser.h"

#define TAB_VIEW_H 36

@interface CTabViewBrowser()
{
    CTabView *_tabView;
    id<CTabViewBrowserDelegate>_delegate;
    UIScrollView *_scrollView;
    NSMutableDictionary *_dicContentView;
    BOOL _moveContentViewInner;
}

@end

@implementation CTabViewBrowser

@synthesize tabView=_tabView;

@synthesize delegate=_delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _tabView = [[CTabView alloc] initWithFrame:CGRectMake(0, 0, self.width, TAB_VIEW_H)];
        _tabView.delegate = self;
        [self addSubview:_tabView];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TAB_VIEW_H, self.width, self.height-TAB_VIEW_H)];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
//        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        
        _dicContentView = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame tabItems:(NSArray *)tabItems;
{
    self = [self initWithFrame:frame];
    if (self)
    {
        [self setTabItems:tabItems];
    }
    return self;
}

- (void)setDelegate:(id<CTabViewBrowserDelegate>)delegate
{
    _delegate = delegate;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (_scrollView != nil)
    {
        _scrollView.contentSize = CGSizeMake(_scrollView.contentSize.width, _scrollView.height);
    }
}

- (void)dealloc
{
    [_tabView release];
    [_scrollView release];
    [_dicContentView release];
    [super dealloc];
}

- (void)setTabItems:(NSArray *)tabItems
{
    [_tabView setTabItems:tabItems];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*tabItems.count, _scrollView.height);
}

- (NSUInteger)currentViewIndex
{
    return self.tabView.curSelectedIndex;
}

- (void)moveToViewIndex:(NSUInteger)index
{
    _moveContentViewInner = NO;
    [self.tabView moveToIndex:index];
}

- (void)moveContentViewToIndex:(NSUInteger)index
{
    [_scrollView scrollRectToVisible:CGRectMake(index*_scrollView.width, 0, _scrollView.width, _scrollView.height) animated:YES];
    [self loadContentViewAtIndex:index];
}

- (void)loadContentViewAtIndex:(NSUInteger)index
{
    self.tabView.curSelectedIndex = index;
    if ([_dicContentView objectForKey:[NSNumber numberWithInteger:index]] == nil)
    {
        UIView *contentView = [self.delegate tabViewBorwser:self contentViewAtIndex:index];
        if (contentView != nil)
        {
            [_dicContentView setObject:contentView forKey:[NSNumber numberWithInteger:index]];
            contentView.frame = CGRectMake(index * _scrollView.width, 0, _scrollView.width, _scrollView.height);
            [_scrollView addSubview:contentView];
        }
    }
    if ([self.delegate respondsToSelector:@selector(tabViewBorwser:didMoveToIndex:)])
    {
        [self.delegate tabViewBorwser:self didMoveToIndex:index];
    }
}

- (void)reloadViewAtIndex:(NSUInteger)index
{
    UIView *contentView = [self.delegate tabViewBorwser:self contentViewAtIndex:index];
    if (contentView != nil)
    {
        [_dicContentView setObject:contentView forKey:[NSNumber numberWithInteger:index]];
        contentView.frame = CGRectMake(index * _scrollView.width, 0, _scrollView.width, _scrollView.height);
        [_scrollView addSubview:contentView];
    }
}

- (CGRect)contentViewBounds
{
    return _scrollView.bounds;
}

- (void)disableItemFromIndex:(NSUInteger)index
{
    [self.tabView disableItemFromIndex:index];
    if (index < self.tabView.itemNum)
    {
        _scrollView.contentSize = CGSizeMake(_scrollView.width*index, _scrollView.height);
}
    else
{
        _scrollView.contentSize = CGSizeMake(_scrollView.width*self.tabView.itemNum, _scrollView.height);
}
}

- (void)disableScroll
{
    _scrollView.scrollEnabled = NO;
}

- (void)enableScroll
{
    _scrollView.scrollEnabled = YES;
}

#pragma mark tab view delegate

- (void)tabView:(CTabView *)tabView willSelectItemAtIndex:(NSUInteger)index
{
    if ([self.delegate respondsToSelector:@selector(tabViewBorwser:willMoveToIndex:)])
    {
        [self.delegate tabViewBorwser:self willMoveToIndex:index];
    }
}

- (void)tabView:(CTabView *)tabView didSelectItemAtIndex:(NSUInteger)index
{
    _moveContentViewInner = NO;
    [self moveContentViewToIndex:index];
}

#pragma mark scroll view delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _moveContentViewInner = YES;
    if ([self.delegate respondsToSelector:@selector(tabViewBorwserContentWillMove:)])
    {
        [self.delegate tabViewBorwserContentWillMove:self];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_moveContentViewInner == YES)
    {
        [self.tabView moveToPositionRate:scrollView.contentOffset.x/scrollView.contentSize.width];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_moveContentViewInner == YES)
    {
        [self loadContentViewAtIndex:(NSInteger)((scrollView.contentOffset.x+1)/scrollView.width)];
    }
}

@end
